package com.pulaien.shop.controller;

import com.github.pagehelper.PageInfo;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.domain.entity.SysUser;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.system.domain.Problem;
import com.pulaien.system.domain.ProblemHandleLog;
import com.pulaien.system.domain.dto.ExtProblemModel;
import com.pulaien.system.mapper.ProblemMapper;
import com.pulaien.system.service.IProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 问题件退货Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/problem")
@Api(tags = "商城端问题件管理")
public class ShopProblemController extends BaseController
{
    @Autowired
    private IProblemService problemService;

    @Autowired
    private ProblemMapper problemMapper;

    @ApiOperation("编辑运单号以及地址")
    @RequestMapping(value = "/updateWaybillNo", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateWaybillNo(@RequestBody ExtProblemModel badListStr) {
        return problemService.updateWaybillNo(badListStr);
    }

    @ApiOperation("新增不良处理说明")
    @RequestMapping(value = "/saveHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveHandleLog(@RequestBody ProblemHandleLog ProblemHandleLog) {
        return problemService.saveHandleLog(ProblemHandleLog);
    }

    @ApiOperation("不良货库处理-正常发送")
    @RequestMapping(value = "/sendNormally", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult sendNormally(@RequestBody Problem badListStr) {
        return problemService.sendNormally(badListStr);
    }

    @ApiOperation("不良货库处理")
    @RequestMapping(value = "/handleProblem", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult handleProblem(@RequestBody Problem badListStr) {
        return problemService.handleProblem(badListStr);
    }

    @ApiOperation("查询不良件货库处理说明")
    @RequestMapping(value = "/handleLogList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult handleLogList(ExtProblemModel queryParam) {
        return problemService.handleLogList(queryParam);
    }

    @ApiOperation("查询问题件货库统计数量")
    @RequestMapping(value = "/getStatisticalNum", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getStatisticalNum(ExtProblemModel queryParam) {
        return AjaxResult.success(problemService.getStatisticalNum(queryParam));
    }

    @ApiOperation("详情页面保存")
    @RequestMapping(value = "/updateProblem", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateProblem(@RequestBody Problem badListStr) {
        return problemService.updateProblem(badListStr);
    }

    @ApiOperation("查询不良件货库详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult detail(@PathVariable Integer id) {

        Problem problemDetail = problemService.detail(id);
        List<String> picList = new ArrayList<>();
        if(StringUtils.isNotBlank(problemDetail.getProblemUrl1())) {
            picList.add(problemDetail.getProblemUrl1());
        }
        if(StringUtils.isNotBlank(problemDetail.getProblemUrl2())) {
            picList.add(problemDetail.getProblemUrl2());
        }
        if(StringUtils.isNotBlank(problemDetail.getProblemUrl3())) {
            picList.add(problemDetail.getProblemUrl3());
        }
        problemDetail.setProblemUrlList(picList);
        return AjaxResult.success(problemDetail);
    }

    @ApiOperation("查询不良件货库")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo list(ExtProblemModel queryParam) {

        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        queryParam.setUserName(currentUser.getUserName());
        startPage();
        List<ExtProblemModel> problemList = problemService.list(queryParam);
        List<ExtProblemModel> problemListNew = new ArrayList<>();
        problemList.forEach(
                problemDetail-> {
                    List<String> picList = new ArrayList<>();
                    if(StringUtils.isNotBlank(problemDetail.getProblemUrl1())) {
                        picList.add(problemDetail.getProblemUrl1());
                    }
                    if(StringUtils.isNotBlank(problemDetail.getProblemUrl2())) {
                        picList.add(problemDetail.getProblemUrl2());
                    }
                    if(StringUtils.isNotBlank(problemDetail.getProblemUrl3())) {
                        picList.add(problemDetail.getProblemUrl3());
                    }
                    problemDetail.setProblemUrlList(picList);
                    problemListNew.add(problemDetail);
                }

        );


        return getDataTable(problemListNew , new PageInfo(problemList).getTotal());
    }

    @ApiOperation("查询待处理的不良件货库")
    @RequestMapping(value = "/getWaitList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getWaitList(ExtProblemModel queryParam) {

        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        queryParam.setUserName(currentUser.getUserName());
        //查询待处理和处理中的问题件
        queryParam.setStateList(Arrays.asList(ConstantsUtil.PROBLEM_STATUS_1,ConstantsUtil.PROBLEM_STATUS_2));
        List<ExtProblemModel> problemList = problemService.list(queryParam);
        return AjaxResult.success(problemList);
    }



    @ApiOperation("不良订单期望处理")
    @GetMapping("/desire")
    public AjaxResult getDesire(@RequestParam("id") Long id, @RequestParam("desire") Integer desire) {

        Problem detail = problemService.getDetail(id);
        detail.setDesire(desire);
        problemMapper.updateById(detail);
        return AjaxResult.success();

    }
}
